package com.ruoyi.cms.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.cms.domain.CmsArticleTags;
import com.ruoyi.cms.domain.bo.CmsArticleTagsBo;
import com.ruoyi.cms.domain.vo.CmsArticleTagsVo;
import com.ruoyi.cms.mapper.CmsArticleTagsMapper;
import com.ruoyi.cms.service.ICmsArticleTagsService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;

/**
 * 文章标签Service业务层处理
 *
 * @author guorui
 * @date 2023-11-08
 */
@RequiredArgsConstructor
@Service
public class CmsArticleTagsServiceImpl implements ICmsArticleTagsService {

    private final CmsArticleTagsMapper baseMapper;

    /**
     * 查询文章标签
     */
    @Override
    public CmsArticleTagsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询文章标签列表
     */
    @Override
    public TableDataInfo<CmsArticleTagsVo> queryPageList(CmsArticleTagsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CmsArticleTags> lqw = buildQueryWrapper(bo);
        Page<CmsArticleTagsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询文章标签列表
     */
    @Override
    public List<CmsArticleTagsVo> queryList(CmsArticleTagsBo bo) {
        LambdaQueryWrapper<CmsArticleTags> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CmsArticleTags> buildQueryWrapper(CmsArticleTagsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CmsArticleTags> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTagId() != null, CmsArticleTags::getTagId, bo.getTagId());
        lqw.eq(bo.getArticleId() != null, CmsArticleTags::getArticleId, bo.getArticleId());
        return lqw;
    }

    /**
     * 新增文章标签
     */
    @Override
    public Boolean insertByBo(CmsArticleTagsBo bo) {
        CmsArticleTags add = BeanUtil.toBean(bo, CmsArticleTags.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改文章标签
     */
    @Override
    public Boolean updateByBo(CmsArticleTagsBo bo) {
        CmsArticleTags update = BeanUtil.toBean(bo, CmsArticleTags.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CmsArticleTags entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除文章标签
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
